/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_05_swing.Example_06_Click_Event1.java]
 * Creation Date 	[22-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 按钮点击事件1
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_06_Click_Event1 {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ButtonFrame1 frame = new ButtonFrame1();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}

}

/**
 * 放置按钮面板的窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ButtonFrame1 extends JFrame {

	private static final long serialVersionUID = -4276293176087454708L;

	/**
	 * 构造方法
	 */
	public ButtonFrame1() {
		this.setTitle("Button Click Event");
		this.setSize(300, 200);
		this.setLocationByPlatform(true);
		this.setResizable(false);
		this.getContentPane().add(new ButtonPanel1());
	}
}

/**
 * 放置按钮的面板
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ButtonPanel1 extends JPanel {

	private static final long serialVersionUID = 6584483859136055453L;

	private JButton redButton = null;
	private JButton blueButton = null;
	private JButton yellowButton = null;

	/**
	 * 构造方法
	 */
	public ButtonPanel1() {
		redButton = new JButton("RED");
		redButton.addActionListener(new ColorActionListener(Color.RED));
		this.add(redButton);

		blueButton = new JButton("BLUE");
		blueButton.addActionListener(new ColorActionListener(Color.BLUE));
		this.add(blueButton);

		yellowButton = new JButton("YELLOW");
		yellowButton.addActionListener(new ColorActionListener(Color.YELLOW));
		this.add(yellowButton);
	}

	private class ColorActionListener implements ActionListener {

		private Color backgroundColor = null;

		public ColorActionListener(Color color) {
			backgroundColor = color;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			ButtonPanel1.this.setBackground(backgroundColor);
		}

	}
}
